import axios from 'axios'
// import { message } from 'ant-design-vue'

// import useUserStore from '@/store/common'
// const userStore = useUserStore()

const instance = axios.create({
  timeout: 10000,
  // 此处一定不要写http,有协议的话，则不会走代理
  baseURL: import.meta.env.VITE_API_URL
  // baseURL: ''
})

// 响应拦截器
instance.interceptors.response.use(
  res => {
    if (204 === res.status) {
      return { code: 0, msg: 'ok', data: null }
    }

    if (String(res.status).startsWith('4')) {
      message.error('接口请求地址有变动，请求异常，请联系开发者', 5)
    }
    if (String(res.status).startsWith('5')) {
      message.error('接口服务器有异常，请联系开发者', 5)
    }

    return res.data
  },
  err => Promise.reject(err)
)

// 请求拦截器
/* instance.interceptors.request.use(config => {

  // config.baseURL
  // 统一去添加token字符串
  // 如果是登录请求 config.url => '/api/login'
  if (!['/api/login'].includes(config.url)) {
    const token = userStore.user.token || ''
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}) */

// export const post = (url, data = {}, config = {}) => instance.post(url, data, config)
//   res => res.data,
//   err => Promise.reject(err)
// )

export const get = (url, config = {}) => instance.get(url, config)
export const post = (url, data = {}, config = {}) => instance.post(url, data, config)
export const del = (url, config = {}) => instance.delete(url, config)
export const patch = (url, data = {}, config = {}) => instance.patch(url, data, config)
export const put = (url, data = {}, config = {}) => instance.put(url, data, config)
